package cn.tedu.luban.settle.main.consumer;

import cn.tedu.luban.order.protocol.dto.OrderMessageDTO;
import cn.tedu.luban.settle.main.service.SettleService;
import com.tedu.inn.protocol.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RocketMQMessageListener(topic = "order_finish_topic",consumerGroup = "settle_group")
public class SettleOrderConsumer implements RocketMQListener<OrderMessageDTO> {
    @Autowired
    private SettleService settleService;
    @Override
    public void onMessage(OrderMessageDTO orderMessageDTO) {
        //添加分布式锁 锁订单 一个订单一个消息 使用订单编号做锁key值
        //抢锁 成功了就执行业务 释放锁
        //抢锁 失败了等待5秒钟重新抢锁 抢到在执行业务
        //由于业务是幂等的,多个线程抢分布式锁 串行执行业务,没有线程安全问题.
        try{
            settleService.settle(orderMessageDTO);
        }catch (BusinessException e){
            log.error("对:{}张订单,执行结算,失败",orderMessageDTO.getOrderNo(),e);
        }
    }
}
